import {
  ATTRIBUTE_TYPES_ENUMS,
  COMMON_IMAGE_STATUS_ENUMS
} from '@/constants/commodity';

export const ATTR_VALUE_KEY = 'newAttributeValueName' // 属性值字段key

// 获取属性值提交所需入参集合-attributeValueVos
export const getAttributeValueVos = (attributeValueUuidList, row) => {
  const selectOptions = row?.attributeValueVos ?? [] // 下拉集合，如果没有默认为空集合
  const attributeValueVos = attributeValueUuidList.map(attributeValueUuid => {
    const valueItem = selectOptions.find(item => item.attributeValueUuid == attributeValueUuid)
    const attributeValueName = valueItem ? valueItem.attributeValueName : attributeValueUuid
    return { attributeValueUuid, attributeValueName }
  })
  return attributeValueVos
}

// 获取attributeValueUuid集合
export const getAttributeValueUuidList = row => {
  if (!row[ATTR_VALUE_KEY]) return []
  const isArray = Array.isArray(row[ATTR_VALUE_KEY])
  const attributeValueUuidList = isArray ? row[ATTR_VALUE_KEY] : [row[ATTR_VALUE_KEY]]
  return attributeValueUuidList
}

// 获取规格属性和基本属性提交入参所需参数
export const getSpecAndNotSpecAttributeVos = dataList => {
  const attributeVos = dataList.map(row => {
    const attributeValueUuidList = getAttributeValueUuidList(row)
    const attributeValueVos = getAttributeValueVos(attributeValueUuidList, row)
    const attributeVoItem = {
      ..._.pick(row, ['skuNo', 'attributeUuid', 'attributeName']),
      attributeValueVos
    }
    return attributeVoItem
  })
  return attributeVos
}

// 获取属性值操作渲染组件
export const getAttrValueRender = (h, row, renderComps) => {
  const [getInputRender, getSelectRender] = renderComps
  const inputRender = getInputRender(h, row) // 文本框
  const radioRender = getSelectRender(h, row, ATTRIBUTE_TYPES_ENUMS.Radio) // 单选下拉框
  const checkboxRender = getSelectRender(h, row, ATTRIBUTE_TYPES_ENUMS.Checkbox) // 多选下拉框
  const radioOrTextRender = getSelectRender(h, row, ATTRIBUTE_TYPES_ENUMS.RadioOrText) // 单选/文本框
  const RenderMaps = {
    [ATTRIBUTE_TYPES_ENUMS.Text]: inputRender,
    [ATTRIBUTE_TYPES_ENUMS.Radio]: radioRender,
    [ATTRIBUTE_TYPES_ENUMS.Checkbox]: checkboxRender,
    [ATTRIBUTE_TYPES_ENUMS.RadioOrText]: radioOrTextRender
  }
  return RenderMaps[row.type]
}

// 获取当前AttributeVos聚合的attributeValueName集合
export const getAttributeValueList = attributeVos => {
  attributeVos = attributeVos ?? []
  const specList = attributeVos.reduce((value, item) => {
    // value.push(...item.attributeValueVos.map(vo => vo.attributeValueName))
    if (item.attributeValueVos.length == 0) {
      value.push({ attributeName: item.attributeName, attributeValueName: '' })
      return value
    }
    item.attributeValueVos.forEach(vo => {
      value.push({ attributeName: item.attributeName, attributeValueName: vo.attributeValueName })
    })
    return value
  }, [])
  return specList
}

// 获取唯一标识
export const getUniqKey = dataVo => `${dataVo?.skuNo}_${dataVo?.attributeUuid ?? dataVo?.attributeName}`

// 获取skuNo与attributeUuid作为唯一标识的集合
export const getAttributeValueVosByUniqKey = (selections, attributeVosKey) => {
  const attributeValueVosMaps = selections.reduce((value, row) => {
    const attributeVos = row[attributeVosKey] ?? []
    attributeVos.forEach(vo => {
      const uniqKey = getUniqKey({ skuNo: row.skuNo, ...vo })
      value[uniqKey] = vo.attributeValueVos
    })
    return value
  }, {})
  return attributeValueVosMaps
}

// 获取填充attributeValue后的属性表格集合
export const getTableListByFillAttributeValue = (tableList, attributeValueVosMaps) => {
  tableList = tableList ? tableList : []
  attributeValueVosMaps = attributeValueVosMaps ? attributeValueVosMaps : {}
  const updateList = tableList.map(row => {
    const uniqKey = getUniqKey(row) // 获取唯一标识
    const attributeValueVos = attributeValueVosMaps[uniqKey] ?? []
    const attributeValueUuids = attributeValueVos.map(item => item.attributeValueUuid)
    const attrValue = row.type == ATTRIBUTE_TYPES_ENUMS.Checkbox
      ? attributeValueUuids
      : attributeValueUuids[0]
    return { ...row, [ATTR_VALUE_KEY]: attrValue }
  })
  return updateList
}

// 获取完善状态文本
export const getCompleteStatusStr = status => {
  const Labels = {
    [COMMON_IMAGE_STATUS_ENUMS.Incomplete]: '未完善',
    [COMMON_IMAGE_STATUS_ENUMS.Perfecting]: '完善中',
    [COMMON_IMAGE_STATUS_ENUMS.Completed]: '已完善',
  }
  return Labels[status] ?? ''
}

// 获取实时规格
export const getRealtimeSpecModel = (row) => {
  const specList = getAttributeValueList(row.specAttributeVos) ?? []
  if (specList.length === 0) return row.specModel ?? '' // 没有规格属性时，显示原规格值
  const specModel = specList
    .map(item => item.attributeValueName)
    .filter(item => item)
    .join('/')
  return specModel
}

// 获取线索转潜在初始后的表格数据
export const getTableListByAddInit = (tableList, selection) => {
  const originalSpecMaps = selection.reduce((value, item) => {
    value[item.skuNo] = item.originalSpecModel
    return value
  }, {})
  const updateList = tableList.map(row => {
    const specModel = originalSpecMaps[row.skuNo] ?? ''
    return { ...row, specModel }
  })
  return updateList
}
